class EventEmitter {
    constructor() {
        this.event = {};
        this.maxListerners = 10;
    }
    // 监听
    on(type, listener) {
        if (this.event[type]) {
            if (this.event[type].length >= this.maxListerners) {
                console.error('同一个监听器最多被十个对象监听,否则可能造成内存泄漏.\n');
                return;
            }
            if (!this.event[type].includes(listener)) {
                this.event[type].push(listener);
            }
        } else {
            this.event[type] = [listener];
        }
    }
    //发送监听
    emit(type, ...rest) {
        if (this.event[type]) {
            this.event[type].map(fn => fn.apply(this, rest));
        }
    }
    //移除监听器
    removeListener(type,func) {
        if (this.event[type]) {
            this.event[type] = this.event[type].filter(item => item !== func);
            if (this.event[type].length === 0) {
                delete this.event[type];
            }
        }
    }
    //移除所有的监听器
    removeAllListener() {
        this.event = {};
    }
}